package com.example.administrator.newsapp;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.newsapp.pojo.Favorite;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.http.bean.Init;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

import static cn.bmob.v3.b.From.S;

public class ShowFavoriteActivity extends AppCompatActivity {

    private ListView mListView;
    private ShowFavoriteAdapter mShowFavoriteAdapter;
    private List<Favorite> mFavoriteList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_favorite);
        mListView =findViewById(R.id.fav_list_view);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("我的收藏");
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initData();

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ShowFavoriteActivity.this,ContentActivity.class);
                String url = mFavoriteList.get(position).getNew_url();
                String image = mFavoriteList.get(position).getNew_image();
                String title = mFavoriteList.get(position).getNew_title();
                intent.putExtra("URL",url);
                intent.putExtra("IMAGE",image);
                intent.putExtra("TITLE",title);
                intent.putExtra("NEWID",mFavoriteList.get(position).getNews_id());
                startActivity(intent);
            }
        });

    }

    private void initData() {
        BmobQuery<Favorite> query = new BmobQuery<>();
        query.findObjects(new FindListener<Favorite>() {
            @Override
            public void done(List<Favorite> list, BmobException e) {
                if (e == null) {
                    for (int i = 0; i < list.size(); i++) {
                        Favorite favorite = list.get(i);
                        if (favorite.getUser_id().equals(BmobUser.getCurrentUser().getObjectId())) {
                            mFavoriteList.add(favorite);
                            mShowFavoriteAdapter=new ShowFavoriteAdapter(mFavoriteList,ShowFavoriteActivity.this);
                            mListView.setAdapter(mShowFavoriteAdapter);
                            mShowFavoriteAdapter.setOnItemDelFavoriteClickListener(new ShowFavoriteAdapter.onItemDelFavoriteListener() {
                                @Override
                                public void onDelFavoriteClick(final int i) {
                                    final Favorite f= mFavoriteList.get(i);
                                    mFavoriteList.remove(i);
                                    mShowFavoriteAdapter.notifyDataSetChanged();
                                    f.delete(new UpdateListener() {
                                        @Override
                                        public void done(BmobException e) {
                                            if(e!=null){return;}
                                        }
                                    });
                                    Snackbar.make(mListView,"是否撤销？",Snackbar.LENGTH_LONG)
                                            .setAction("是", new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    f.save(new SaveListener<String>() {
                                                        @Override
                                                        public void done(String s, BmobException e) {
                                                            Toast.makeText(ShowFavoriteActivity.this,"撤销成功",Toast.LENGTH_SHORT).show();
                                                        }
                                                    });
                                                    mFavoriteList.add(f);
                                                    mShowFavoriteAdapter.notifyDataSetChanged();
                                                }
                                            })
                                            .show();
                                }
                            });
                        }
                    }
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
